package com.ruoyi.project.shop.service.imp;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.DataScope;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.common.utils.SnowflakeIdWorker;
import com.ruoyi.project.custom.domain.CustomInfo;
import com.ruoyi.project.custom.mapper.CustomInfoMapper;
import com.ruoyi.project.shop.domain.ShopInfo;
import com.ruoyi.project.shop.domain.ShopOperator;
import com.ruoyi.project.shop.mapper.ShopInfoMapper;
import com.ruoyi.project.shop.mapper.ShopOperatorMapper;
import com.ruoyi.project.shop.service.IShopInfoService;
import com.ruoyi.project.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ShopInfoServiceImpl implements IShopInfoService {

    @Autowired
    private ShopInfoMapper shopMapper;
    @Autowired
    private ShopOperatorMapper operatorMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private CustomInfoMapper customMapper;

    @Override
    @DataScope(deptAlias = "u", userAlias = "u")
    public List<ShopInfo> selectShopList(ShopInfo shop) {
        return shopMapper.selectShopList(shop);
    }

    @Override
    public ShopInfo selectShopById(String id) {
        return shopMapper.selectShopById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertShop(ShopInfo shop) {
        // 先检查是否已经存在相同账号的店铺了（店铺账号唯一）
        if(shopMapper.checkShopByAccount(shop) > 0){
            return AjaxResult.error("已存在相同的店铺账号了！");
        }

        shop.setId(SnowflakeIdWorker.generateId().toString());
        shop.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserId().toString());
        shop.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId().toString());
        shopMapper.insertShop(shop);

        ShopOperator operator = new ShopOperator();
        operator.setShopId(shop.getId());
        operator.setUserId(SecurityUtils.getLoginUser().getUser().getUserId().toString());
        operatorMapper.insertShopOperator(operator);

        return AjaxResult.success("新增店铺成功！");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int importShop(List<ShopInfo> shopInfoList) {
        if(shopInfoList != null && shopInfoList.size() > 0){
            for(ShopInfo shop : shopInfoList){
                shop.setId(SnowflakeIdWorker.generateId().toString());
                shop.setName(shop.getName().trim());
                shop.setShopUrl(shop.getShopUrl().trim());
                shop.setAccount(shop.getAccount().trim());
                shop.setWechatGroup(shop.getWechatGroup().trim());
                shop.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserId().toString());
                shop.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId().toString());

                CustomInfo customInfo =customMapper.selectCustomByName(shop.getCusName().trim());
                if(customInfo != null){
                    shop.setCustomId(customInfo.getId());
                }
                shopMapper.insertShop(shop);

                ShopOperator operator = new ShopOperator();
                operator.setShopId(shop.getId());
                operator.setUserId(SecurityUtils.getLoginUser().getUser().getUserId().toString());
                operatorMapper.insertShopOperator(operator);
            }
            return 1;
        }
        return 0;
    }

    @Override
    public int checkShopByAccount(ShopInfo shopInfo) {
        return shopMapper.checkShopByAccount(shopInfo);
    }

    @Override
    public AjaxResult updateShop(ShopInfo shop) {
        if(shopMapper.checkShopByAccount(shop) > 0){
            return AjaxResult.error("已存在相同的店铺账号了！");
        }

        shop.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId().toString());

        return AjaxResult.success(shopMapper.updateShop(shop));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteShop(String id) {
        operatorMapper.deleteShopOperator(id);
        shopMapper.deleteShop(id);
        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult assignOperators(List<ShopOperator> operators) {
        if(operators != null && operators.size() > 0){
            for(ShopOperator operator : operators){
                ShopOperator shopOperator = operatorMapper.selectShopOperator(operator.getShopId(), operator.getUserId());
                if(shopOperator == null) {
                    operatorMapper.insertShopOperator(operator);
                }
            }
        }
        return AjaxResult.success("分配运营人员成功！");
    }

    @Override
    public AjaxResult transferOperators(List<ShopOperator> operators) {
        if(operators != null && operators.size() > 0){
            List<String> shopIds = operators.stream().map(ShopOperator::getShopId).distinct().collect(Collectors.toList());
            for (String shopId : shopIds){
                operatorMapper.deleteShopOperatorByUserId(shopId, SecurityUtils.getLoginUser().getUser().getUserId().toString());
            }

            for(ShopOperator operator : operators){
                ShopOperator shopOperator = operatorMapper.selectShopOperator(operator.getShopId(), operator.getUserId());
                if(shopOperator == null) {
                    operatorMapper.insertShopOperator(operator);
                }
            }
        }
        return AjaxResult.success("分配运营人员成功！");
    }
}
